import Footer from '@/components/Footer';
import {LockOutlined, UserOutlined,} from '@ant-design/icons';
import {LoginForm, ProFormText,} from '@ant-design/pro-components';
import {message, Tabs} from 'antd';
import React, {useState} from 'react';
import {history} from 'umi';
import styles from './index.less';
import {register} from "@/services/ant-design-pro/api";

const Register: React.FC = () => {
    const [type, setType] = useState<string>('account');

    const handleSubmit = async (values: API.RegisterParams) => {
      const {userPassword, checkPassword} = values;
      // 校验
      if (userPassword != checkPassword) {
        message.error('两次输入的密码不一致');
        return;
      }
      try {
        // 注册
        const id = await register(values);
        if (id) {
          const defaultLoginSuccessMessage = '注册成功！';
          message.success(defaultLoginSuccessMessage);
          /** 此方法会跳转到 redirect 参数所在的位置 */
          if (!history) return;
          const {query} = history.location;
          history.push({
            pathname: '/user/login',
            query
          });
          return;
        }
      } catch (error: any) {
        const defaultLoginFailureMessage = '注册失败，请重试！';
        message.error(defaultLoginFailureMessage);
      }
    };

    return (
      <div className={styles.container}>
        <div className={styles.content}>
          <LoginForm
            submitter={{
              searchConfig: {submitText: '注册'}
            }}
            logo={<img alt="logo" src="/200X200.png"/>}
            title="User Center"
            subTitle={'User Center 是基于SpringBoot的项目'}
            initialValues={{
              autoLogin: true,
            }}
            onFinish={async (values) => {
              await handleSubmit(values as API.RegisterParams);
            }}
          >
            <Tabs activeKey={type} onChange={setType}>
              <Tabs.TabPane key="account" tab={'账号密码注册'}/>
            </Tabs>
            {type === 'account' && (
              <>
                <ProFormText
                  name="userAccount"
                  fieldProps={{
                    size: 'large',
                    prefix: <UserOutlined className={styles.prefixIcon}/>,
                  }}
                  placeholder="请输入账号"
                  rules={[
                    {
                      required: true,
                      message: '账号是必填项！',
                    },
                  ]}
                />
                <ProFormText.Password
                  name="userPassword"
                  fieldProps={{
                    size: 'large',
                    prefix: <LockOutlined className={styles.prefixIcon}/>,
                  }}
                  placeholder="请输入密码"
                  rules={[
                    {
                      required: true,
                      message: '密码是必填项！',
                    },
                    {
                      min: 8,
                      type: 'string',
                      message: '密码长度不能小于8',
                    },
                  ]}
                />
                <ProFormText.Password
                  name="checkPassword"
                  fieldProps={{
                    size: 'large',
                    prefix: <LockOutlined className={styles.prefixIcon}/>,
                  }}
                  placeholder="确认密码"
                  rules={[
                    {
                      required: true,
                      message: '确认密码是必填项！',
                    },
                    {
                      min: 8,
                      type: 'string',
                      message: '密码长度不能小于8',
                    }
                  ]}
                />
              </>
            )}
          </LoginForm>
        </div>
        <Footer/>
      </div>
    );
  }
;
export default Register;
